package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.zkb.annotation.Cacheable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "专辑详情管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class itemApiController {

	@Autowired
	private ItemService itemService;
	@PostMapping("/onAlbumEs/{albumId}")
	@Operation(summary = "上架专辑到es")
	public Result onAlbumEs(@PathVariable(value = "albumId") Long albumId) {
		itemService.onAlbumEs(albumId);
		return Result.ok();
	}

	@PostMapping("/offAlbumEs/{albumId}")
	@Operation(summary = "下架专辑到es")
	public Result offAlbumEs(@PathVariable(value = "albumId") Long albumId) {

		itemService.offAlbumEs(albumId);
		return Result.ok();
	}

	@PostMapping("/batch/onAlbumEsList")
	@Operation(summary = "批量上架专辑到es")
//	@TingShuLogin
	public Result onAlbumEs() {
		 // 从MySQL中查询专辑的ids
		itemService.onAlbumEsList();
		return Result.ok();
	}

	@GetMapping("{albumId}")
	@Operation(summary = "专辑详情")
	@SneakyThrows
	@Cacheable(cacheKey = RedisConstant.CACHE_INFO_PREFIX + "#{#args[0]}",
			distroLockKey = RedisConstant.CACHE_LOCK_SUFFIX + "#{#args[0]}",
			distroBloomKey = "#{#args[0]}",
			distroBloomSwitch = true,
			distroLockSwitch = true)
	public Result getAlbumDetail(@PathVariable(value = "albumId") Long albumId){
		Map<String,Object> map = itemService.getAlbumDetail(albumId);
		return Result.ok(map);
	}
	@GetMapping("/preHitRank")
	@Operation(summary = "排行榜数据的提前预热")
	public Result preHitRank() {
		itemService.preHitRank();
		return Result.ok();
	}
	//Request URL: http://localhost:8500/api/search/albumInfo/findRankingList/1/hotScore
	@GetMapping("/findRankingList/{c1Id}/{dimension}")
	@Operation(summary = "专辑排行榜")
	public Result findRankingList(@PathVariable("c1Id") Long c1Id,
								  @PathVariable("dimension") String dimension){
		List<AlbumInfoIndexVo> albumInfoIndexVoList = itemService.findRankingList(c1Id,dimension);
		return Result.ok(albumInfoIndexVoList);
	}
}

